import json

def search_key(db, a, b, value_key: str): #a 是年份，b是城市
    if value_key in db:
        return [str(db[value_key]),value_key]
    elif a + value_key in db:
        return [str(db[a + value_key]),a + value_key]
    elif b + value_key in db:
        return [str(db[b + value_key]),b + value_key]
    elif a + b + value_key in db:
        return [str(db[a + b + value_key]),a + b + value_key]
    else:
        return None
    
def insert_list(a, b, index):
    a.pop(index)
    b.insert(0, "(")
    b.append(")")
    for i, item in enumerate(b):
        a.insert(index + i, item)
    if a.count("(") != a.count(")"):
        print("公式中括号不匹配")
    return a

def read_txt(file_name):
    result = []
    with open(file_name, 'r',encoding='UTF-8') as f:
        lines = f.readlines()
        for x in lines:
            x = x.strip()
            x = x.replace("城区","")
            x = x.replace("城城","城") #**县城 城区... 改为 **县 城区...
            result.append(x)
    return result
 
def read_json(file_name)->dict:
    with open(file_name, 'r',encoding='UTF-8') as f:
        result = json.load(f)
    return result

def str_to_expression(expr_str:str)->list:
    expr_str = expr_str.replace(" ", "") #去除公式中所有的空格
    operators = ['+', '-', '*', '/', '(', ')', ',']
    tokens = []
    current_token = ""
    for char in expr_str:
        if char in operators:
            if current_token:
                tokens.append(current_token)
                current_token = ""
            if char == ',':
                tokens.append(expr_str.split(',')[1])
                return tokens
            else:
                tokens.append(char)
        else:
            current_token += char
    if current_token:
        tokens.append(current_token)
    
    return tokens + ["单位"] #没有单位时补上“单位”两个字作为单位


if __name__ == '__main__':
    pass
